package com.wolfalpha.utron.service.impl;

import android.text.TextUtils;
import android.util.Log;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.Idea;
import com.wolfalpha.utron.bean.IdeaComment;
import com.wolfalpha.utron.bean.ItemIdea;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.IdeaService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodon on 15/8/20.
 */
public class IdeaServiceImpl implements IdeaService {

    private final static String TAG = "IdeaServiceImpl";

    @Override
    public List<ItemIdea> getList(int page, int order, List<Integer> dirs) {
        List<ItemIdea> ideas = new ArrayList<ItemIdea>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        if (order == 0) {
            mp.put("order", "time");
        }
        if (order == 1) {
            mp.put("order", "hot");
        }
        if (order == 2 && dirs != null) {
            mp.put("order", "dir");
            for (int i = 0; i < dirs.size(); i++)
                mp.put("dirs[" + String.valueOf(i) + "]", String.valueOf(dirs.get(i)));
        }
        if (order == 3) {
            mp.put("order", "rand");
        }
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "getList", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }

        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemIdea idea = JsonUtils.paraseJson(arr.get(i).toString(), ItemIdea.class);
                ideas.add(idea);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ideas;
    }

    @Override
    public List<ItemIdea> getMyList(int userId, int page) {
        List<ItemIdea> ideas = new ArrayList<ItemIdea>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        mp.put("user_id", String.valueOf(userId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "getMyList", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemIdea idea = JsonUtils.paraseJson(arr.get(i).toString(), ItemIdea.class);
                ideas.add(idea);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ideas;
    }

    @Override
    public Idea getDetail(int Ideaid) {
        Map<String, String> mp = new HashMap<>();
        mp.put("idea_id", String.valueOf(Ideaid));
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "getDetail", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), Idea.class);
    }

    @Override
    public boolean praise(int ideaId, boolean status) {
        Map<String, String> mp = new HashMap<>();
        mp.put("idea_id", String.valueOf(ideaId));
        mp.put("do", status ? "good" : "bad");
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "do", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean doComment(int ideaId, String content, int toId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("idea_id", String.valueOf(ideaId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("to_id", String.valueOf(toId));
        mp.put("content", content);
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "comment", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean doWillJoin(int ideaId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("idea_id", String.valueOf(ideaId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "will", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean delete(int ideaId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("idea_id", String.valueOf(ideaId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("password", String.valueOf(AppContext.getInstance().getProperty("utron_pwd")));
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "delete", mp);
        if (result == null) {
            return false;
        }
        Log.i(TAG, result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean publish(String what, String how, String why, int dir) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("what", what);
        mp.put("how", how);
        mp.put("why", why);
        mp.put("dir", String.valueOf(dir));
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "publish", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public List<IdeaComment> getComments(int ideaId, int page, int toId) {
        List<IdeaComment> ideas = new ArrayList<IdeaComment>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        mp.put("idea_id", String.valueOf(ideaId));
        mp.put("to_id", String.valueOf(toId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.IDEA_URL + "getComments", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                IdeaComment comment = JsonUtils.paraseJson(arr.get(i).toString(), IdeaComment.class);
                ideas.add(comment);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ideas;
    }
}
